package com.zk.nakedfowl.barcodeGrantManager.service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import com.zk.nakedfowl.common.util.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;


import com.zk.nakedfowl.barcodeGrantManager.dao.RecoveryDao;
import com.zk.nakedfowl.barcodeGrantManager.pojo.Recovery;

/**
 * 服务层
 * 
 * @author Administrator
 *
 */
@Service
public class RecoveryService {

	@Autowired
	private RecoveryDao recoveryDao;
	
	@Autowired
	private IdWorker idWorker;

	/**
	 * 查询全部列表
	 * @return
	 */
	public List<Recovery> findAll() {
		return recoveryDao.findAll();
	}

	
	/**
	 * 条件查询+分页
	 * @param whereMap
	 * @param page
	 * @param size
	 * @return
	 */
	public Page<Recovery> findSearch(Map whereMap, int page, int size) {
		Specification<Recovery> specification = createSpecification(whereMap);
		PageRequest pageRequest =  PageRequest.of(page-1, size);
		return recoveryDao.findAll(specification, pageRequest);
	}

	
	/**
	 * 条件查询
	 * @param whereMap
	 * @return
	 */
	public List<Recovery> findSearch(Map whereMap) {
		Specification<Recovery> specification = createSpecification(whereMap);
		return recoveryDao.findAll(specification);
	}

	/**
	 * 根据ID查询实体
	 * @param id
	 * @return
	 */
	public Recovery findById(String id) {
		return recoveryDao.findById(id).get();
	}

	/**
	 * 增加
	 * @param recovery
	 */
	public void add(Recovery recovery) {
		Calendar calendar= Calendar.getInstance();
		SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd :hh:mm:ss");
		String tmp = dateFormat.format(calendar.getTime());
		try{
			Date data = dateFormat.parse(tmp);
			recovery.setCreateDate(data);
		}catch (ParseException e){
			System.out.println(e);
		}
		recovery.setId( idWorker.nextId()+"" );
		recoveryDao.save(recovery);
	}

	/**
	 * 修改
	 * @param recovery
	 */
	public void update(Recovery recovery) {
		recoveryDao.save(recovery);
	}

	/**
	 * 删除
	 * @param id
	 */
	public void deleteById(String id) {
		recoveryDao.deleteById(id);
	}

	/**
	 * 动态条件构建
	 * @param searchMap
	 * @return
	 */
	private Specification<Recovery> createSpecification(Map searchMap) {

		return new Specification<Recovery>() {

			@Override
			public Predicate toPredicate(Root<Recovery> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> predicateList = new ArrayList<Predicate>();
                // 
                if (searchMap.get("id")!=null && !"".equals(searchMap.get("id"))) {
                	predicateList.add(cb.like(root.get("id").as(String.class), "%"+(String)searchMap.get("id")+"%"));
                }
                // 条形码编号（预留字段）
                if (searchMap.get("barcodeId")!=null && !"".equals(searchMap.get("barcodeId"))) {
                	predicateList.add(cb.like(root.get("barcodeId").as(String.class), "%"+(String)searchMap.get("barcodeId")+"%"));
                }
                // 发放档口编号
                if (searchMap.get("stallsId")!=null && !"".equals(searchMap.get("stallsId"))) {
                	predicateList.add(cb.like(root.get("stallsId").as(String.class), "%"+(String)searchMap.get("stallsId")+"%"));
                }
                // 回收人员id
                if (searchMap.get("recoveryPerson")!=null && !"".equals(searchMap.get("recoveryPerson"))) {
                	predicateList.add(cb.like(root.get("recoveryPerson").as(String.class), "%"+(String)searchMap.get("recoveryPerson")+"%"));
                }
                // 送回人员id
                if (searchMap.get("sendbackPerson")!=null && !"".equals(searchMap.get("sendbackPerson"))) {
                	predicateList.add(cb.like(root.get("sendbackPerson").as(String.class), "%"+(String)searchMap.get("sendbackPerson")+"%"));
                }
                // 退回原因
                if (searchMap.get("sendbackCause")!=null && !"".equals(searchMap.get("sendbackCause"))) {
                	predicateList.add(cb.like(root.get("sendbackCause").as(String.class), "%"+(String)searchMap.get("sendbackCause")+"%"));
                }
                // 创建者（发放人员）
                if (searchMap.get("createBy")!=null && !"".equals(searchMap.get("createBy"))) {
                	predicateList.add(cb.like(root.get("createBy").as(String.class), "%"+(String)searchMap.get("createBy")+"%"));
                }
				
				return cb.and( predicateList.toArray(new Predicate[predicateList.size()]));

			}
		};

	}

}
